#ifndef __LCD1602_H
#define __LCD1602_H
#include "sys.h"

// address define of command and reg.
#define     LCD_POS_BASE                0x80

#define     LCD_ROW_1                   0x00 + LCD_POS_BASE
#define     LCD_ROW_2                   0x40 + LCD_POS_BASE

#define     LCD_COL_1                   0x00
#define     LCD_COL_2                   0x01
#define     LCD_COL_3                   0x02
#define     LCD_COL_4                   0x03
#define     LCD_COL_5                   0x04
#define     LCD_COL_6                   0x05
#define     LCD_COL_7                   0x06
#define     LCD_COL_8                   0x07
#define     LCD_COL_9                   0x08
#define     LCD_COL_10                   0x09
#define     LCD_COL_11                   0x0A
#define     LCD_COL_12                   0x0B
#define     LCD_COL_13                   0x0C
#define     LCD_COL_14                   0x0D
#define     LCD_COL_15                   0x0E
#define     LCD_COL_16                   0x0F
#define     LCD_COL_17                   0x10
#define     LCD_COL_18                   0x11
#define     LCD_COL_19                   0x12
#define     LCD_COL_20                   0x13
#define     LCD_COL_21                   0x14
#define     LCD_COL_22                   0x15
#define     LCD_COL_23                   0x16
#define     LCD_COL_24                   0x17
#define     LCD_COL_25                   0x18
#define     LCD_COL_26                   0x19
#define     LCD_COL_27                   0x1A
#define     LCD_COL_28                   0x1B
#define     LCD_COL_29                   0x1C
#define     LCD_COL_30                   0x1D
#define     LCD_COL_31                   0x1E
#define     LCD_COL_32                   0x1F
#define     LCD_COL_33                   0x20
#define     LCD_COL_34                   0x21
#define     LCD_COL_35                   0x22
#define     LCD_COL_36                   0x23
#define     LCD_COL_37                   0x24
#define     LCD_COL_38                   0x25
#define     LCD_COL_39                   0x26
#define     LCD_COL_40                   0x27

// port and pin initial.
#define     GPIO_LCD_RS                  GPIOB
#define     PIN_LCD_RS                   GPIO_PIN_1
#define     RCC_INIT_LCD_RS()            __HAL_RCC_GPIOB_CLK_ENABLE()
#define     LCD_RS(x)                    do{ \
                                            x ? \
                                            HAL_GPIO_WritePin(GPIO_LCD_RS, PIN_LCD_RS, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(GPIO_LCD_RS, PIN_LCD_RS, GPIO_PIN_RESET); \
                                            } while(0U)

#define     GPIO_LCD_RW                  GPIOB
#define     PIN_LCD_RW                   GPIO_PIN_2
#define     RCC_INIT_LCD_RW()            __HAL_RCC_GPIOB_CLK_ENABLE()
#define     LCD_RW(x)                    do{ \
                                            x ? \
                                            HAL_GPIO_WritePin(GPIO_LCD_RW, PIN_LCD_RW, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(GPIO_LCD_RW, PIN_LCD_RW, GPIO_PIN_RESET); \
                                            } while(0U)

#define     GPIO_LCD_EN                  GPIOB
#define     PIN_LCD_EN                   GPIO_PIN_10
#define     RCC_INIT_LCD_EN()            __HAL_RCC_GPIOB_CLK_ENABLE()
#define     LCD_EN(x)                    do{ \
                                            x ? \
                                            HAL_GPIO_WritePin(GPIO_LCD_EN, PIN_LCD_EN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(GPIO_LCD_EN, PIN_LCD_EN, GPIO_PIN_RESET); \
                                            } while(0U)


#define     GPIO_LCD_D0                  GPIOA
#define     PIN_LCD_D0                   GPIO_PIN_0
#define     RCC_INIT_LCD_D0()            __HAL_RCC_GPIOA_CLK_ENABLE()

#define     GPIO_LCD_D1                  GPIOA
#define     PIN_LCD_D1                   GPIO_PIN_1
#define     GPIO_LCD_D2                  GPIOA
#define     PIN_LCD_D2                   GPIO_PIN_2
#define     GPIO_LCD_D3                  GPIOA
#define     PIN_LCD_D3                   GPIO_PIN_3
#define     GPIO_LCD_D4                  GPIOA
#define     PIN_LCD_D4                   GPIO_PIN_4
#define     GPIO_LCD_D5                  GPIOA
#define     PIN_LCD_D5                   GPIO_PIN_5
#define     GPIO_LCD_D6                  GPIOA
#define     PIN_LCD_D6                   GPIO_PIN_6
#define     GPIO_LCD_D7                  GPIOA
#define     PIN_LCD_D7                   GPIO_PIN_7


void lcd_init(void);
void lcd_start_init(void);

void lcd_write_cmd(uint8_t cmd);
void lcd_write_data(uint8_t data);

uint8_t lcd_read_cmd(void);
//uint8_t lcd_read_data(void);
uint8_t is_busy_lcd(void);

void lcd_clear_all_char(void);

void lcd_show_char(uint8_t row, uint8_t col, char data);
void lcd_show_string(uint8_t row, uint8_t col, char *str);

#endif

